$(function () {
    var settings = {
        url: ctx + "role/list",
        pageSize: 10,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                roleName: ""
            };
        },
        columns: [{
            checkbox: true
        }, {
            field: 'roleName',
            title: '角色'
        }, {
            field: 'remark',
            title: '描述'
        }, {
            field: 'createTime',
            title: '创建时间'
        }]
    };
    $JuXue.initTable('role-data-table', settings);

    $.post(ctx + "menu/menuButtonTree", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#menuTreeedit').jstree({
                "core": {
                    "data":data.children
                },
                "checkbox" : {
                    "keep_selected_style" : false
                },
                "plugins" : [ "wholerow", "checkbox" ]
            });
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
});
/**查询*/
function role_search() {
    $JuXue.refreshTable('role-data-table');
}
/**刷新*/
function role_refresh() {
    role_search();
}
/**删除角色**/
function deleteRoles() {
    var selected = $("#role-data-table").bootstrapTable('getSelections');
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要删除的角色！');return;}
    var ids = "";
    for (var i = 0; i < selected_length; i++) {
        ids += selected[i].roleId;
        if (i !== (selected_length - 1)) ids += ",";
    }
    $JuXue.confirm({
        text: "删除选中角色将导致该角色对应账户失去相应的权限，确定删除？",
        confirmButtonText: "确定删除"
    }, function () {
        $.post(ctx + 'role/delete', {"ids": ids}, function (r) {
            if (r.code === 0) {
                $JuXue.n_success(r.msg);
                role_refresh();
            } else {
                $JuXue.n_danger(r.msg);
            }
        });
    });
}
/**导出角色**/
function exportRoleExcel() {
    $.post(ctx + "role/excel", $(".role-table-form").serialize(), function (r) {
        if (r.code === 0) {
            window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;
        } else {
            $JuXue.n_warning(r.msg);
        }
    });
}
/**新增角色信息**/
function addRoles() {
    $.post(ctx+"role/addRoleDialog",{},function (r) {$("#Roles_info").empty().append(r).modal("show").draggable();})
}
/**编辑角色信息**/
function editRoles() {
    $("#Roles_info").empty();
    var selected = $("#role-data-table").bootstrapTable('getSelections');
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的角色！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个角色！');return;}
    $("#Roles_info").empty();
    $("#Roles_info_edit").modal("show").draggable();

    $("#menuTreeedit").jstree().deselect_all(true);
    var roleId = selected[0].roleId;
    $("#roleId").val(roleId);
    $.post(ctx + "role/getRole", {"roleId": roleId}, function (r) {
        if (r.code === 0) {
            var $menuTree = $('#menuTreeedit');
            var role = r.msg;
            $("#roleNameedit").val(role.roleName);
            $("#remarkedit").val(role.remark);
            $("#roleIdedit").val(role.roleId);
            var menuArr = new Array();
            for (var i = 0; i < role.menuIds.length; i++) {
                menuArr.push(role.menuIds[i]);
            }
            $menuTree.jstree().select_all(true);
            $menuTree.jstree().deselect_node (menuArr,true);
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}

/**保存角色的更新**/
function editRolesInfo() {
    var $menuTree = $('#menuTreeedit');
    var ref = $menuTree.jstree(true);
    var menuIds = ref.get_checked();
    $menuTree.find(".jstree-undetermined").each(function (i, element) {
        menuIds.push($(element).closest('.jstree-node').attr("id"));
    });
    $("#menuId").val(menuIds);
    var $roleAddForm = $("#role-edit-form");
    $.post(ctx + "role/update", $roleAddForm.serialize(), function (r) {
        if (r.code === 0) {
            $JuXue.n_success(r.msg);
            $JuXue.refreshTable("role-data-table");
            $("#Roles_info_edit").modal("hide");
        } else $JuXue.n_danger(r.msg);
    });

}